﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace PhoneApp1
{
    public enum ItemType
    {
        Tower = 1,
        Enemy = 2,
        zd = 3,
    }
    public class Item : IDisposable
    {
        private static long _MaxID;
        public static long MaxID
        {
            get
            {
                _MaxID++;
                return _MaxID;
            }
        }
        public long ID { get; set; }
        public ItemType Type { get; set; }
        public string Image { get; set; }
        public int x { get; set; }
        public int y { get; set; }
        public Point Center { get; set; }
        public int z { get; set; }
        public int width { get; set; }
        public int height { get; set; }
        public double sd { get; set; }
        public Shape ThisShape { get; set; }
        public GameBase Game { get; set; }
        public Timer timer { get; set; }
        public delegate void MouseClick(object o, MouseButtonEventArgs e);
        public Item(GameBase game)
        {
            this.Game = game;
        }

        public void RegisterEvents()
        {
            ThisShape.MouseLeftButtonUp += ThisShape_MouseLeftButtonUp;
            ThisShape.GotFocus += ThisShape_GotFocus;
            ThisShape.MouseLeftButtonDown += ThisShape_MouseLeftButtonDown;
        }

        void ThisShape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ThisShape.Stroke = new SolidColorBrush(Colors.Brown);
            ThisShape.StrokeThickness = 1;
            this.Game.SelectedItem = this;
        }

        void ThisShape_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            ThisShape.Stroke = new SolidColorBrush(Colors.Brown);
            ThisShape.StrokeThickness = 1;
        }

        void ThisShape_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

            SetSelected();
            e.Handled = true;
        }

        public virtual void SetSelected()
        {
            this.Game.SelectedItem = this;
            ThisShape.Stroke = new SolidColorBrush(Colors.Brown);
            ThisShape.StrokeThickness = 1;
        }
        public virtual void SetNoSelected()
        {
            this.Game.SelectedItem = null;
            ThisShape.Stroke = new SolidColorBrush(Colors.Brown);
            ThisShape.StrokeThickness = 0;
        }

        public virtual void Draw()
        {

        }


        public void Dispose()
        {
            if (this.timer != null)
                this.timer.Dispose();
            Game.GameMap.Dispatcher.BeginInvoke(() =>
            {
                Game.GameMap.Children.Remove(this.ThisShape);
                this.ThisShape = null;
                this.Game.RemoveItem(this);
            });

        }
    }



}
